package com.ytjj.quartz.task;


import cn.hutool.http.HttpUtil;
import com.ytjj.common.utils.feishu.FeiShuUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 服务健康检查
 */
@Component
@Slf4j
@RefreshScope
public class CheckTask {

    @Value("${short.platfrom.url:}")
    private String url;

    @Value("${heartbeat.url:}")
    private String checkUrl;

    @Value("${serverName:}")
    private String serverName;

    @Scheduled(cron = "0/30 * * * * ?")
    public void checkShort() {
        if (StringUtils.isNotBlank(url)){
            try {
                String result = HttpUtil.get(url+"/admin/check");
                if (result == null || !"ok".equals(result)){
                    FeiShuUtil.sendYwMsg(String.format("%s,实例不健康,请检查(%s)",url,serverName));
                }
            } catch (Exception e) {
                log.error("{}",e);
                FeiShuUtil.sendYwMsg(String.format("%s,实例不健康,请检查(%s)",url,serverName));
            }
        }
        if (StringUtils.isNotBlank(checkUrl)){
            String[] urlArr = checkUrl.split(",");
            for (String item : urlArr) {
                try {
                    if (item!=null && item.contains("yiquan-keji.com")){
                        continue;
                    }
                    String result = HttpUtil.get(item);
                    if (result == null || !"ok".equals(result)){
                        FeiShuUtil.sendYwMsg(String.format("%s,实例不健康,请检查(%s)",item,serverName));
                    }
                } catch (Exception e) {
                    log.error("{}",e);
                    FeiShuUtil.sendYwMsg(String.format("%s,实例不健康,请检查(%s)",item,serverName));
                }
            }
        }
    }

    public static void main(String[] args) {
        String result = HttpUtil.get("https://XXX.myjckjsz.com/mall/test/check");
        System.out.println(result);
    }
}
